package com.demo.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author hz
 * @date 2023年07月25日 15:58
 */
@Component
public class TestTask {

    public static void main(String[] args) {
        Random random = new Random();

        while (true) {
            try {
                int sleepTime = random.nextInt(10) + 1; // 生成1到10之间的随机数
                System.out.println("线程阻塞时间：" + sleepTime + "秒");
                Thread.sleep(sleepTime * 1000); // 将秒转换为毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    @Scheduled(cron ="*/10 * * * * ?")
    public void sayWord() throws InterruptedException {
        System.out.println("world");
        Random random = new Random();

        while (true){
            try {
                int sleepTime = (int) (Math.random() * 116) + 5; // 生成5-120之间的随机数
                //int sleepTime = random.nextInt(10) + 1; // 生成1到10之间的随机数
                System.out.println("线程阻塞时间：" + sleepTime + "秒");
                Thread.sleep(sleepTime * 1000); // 将秒转换为毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt(); // 重新中断当前线程
            }
        }
       // System.out.println("end");
    }

}
